package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebMallProduct;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWebMallProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商城商品Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-14
 */
@Controller
@RequestMapping("/system/mallProduct")
public class WebMallProductController extends BaseController
{
    private String prefix = "system/mallProduct";

    @Autowired
    private IWebMallProductService webMallProductService;
    @Autowired
    private ISysConfigService iSysConfigService;

    @RequiresPermissions("system:mallProduct:view")
    @GetMapping()
    public String mallProduct()
    {
        return prefix + "/mallProduct";
    }

    /**
     * 查询商城商品列表
     */
    @RequiresPermissions("system:mallProduct:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebMallProduct webMallProduct)
    {
        startPage();
        List<WebMallProduct> list = webMallProductService.selectWebMallProductList(webMallProduct);
        return getDataTable(list);
    }

    /**
     * 导出商城商品列表
     */
    @RequiresPermissions("system:mallProduct:export")
    @Log(title = "商城商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebMallProduct webMallProduct)
    {
        List<WebMallProduct> list = webMallProductService.selectWebMallProductList(webMallProduct);
        ExcelUtil<WebMallProduct> util = new ExcelUtil<WebMallProduct>(WebMallProduct.class);
        return util.exportExcel(list, "商城商品数据");
    }

    /**
     * 新增商城商品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商城商品
     */
    @RequiresPermissions("system:mallProduct:add")
    @Log(title = "商城商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebMallProduct webMallProduct)
    {
        return toAjax(webMallProductService.insertWebMallProduct(webMallProduct));
    }

    /**
     * 修改商城商品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebMallProduct webMallProduct = null;
        if(id == null || id ==  -1){
            webMallProduct = new WebMallProduct();
        }else{
            webMallProduct = webMallProductService.getById(id);
        }
        mmap.put("score_value",iSysConfigService.selectConfigByKey("score_value"));
        mmap.put("webMallProduct", webMallProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存商城商品
     */
    @RequiresPermissions("system:mallProduct:edit")
    @Log(title = "商城商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebMallProduct webMallProduct)
    {
        return toAjax(webMallProductService.updateWebMallProduct(webMallProduct));
    }

    /**
     * 删除商城商品
     */
    @RequiresPermissions("system:mallProduct:remove")
    @Log(title = "商城商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webMallProductService.deleteWebMallProductByIds(ids));
    }
}
